package com;



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DBConnection;

/**
 * Servlet implementation class MasterQuickServlet
 */
@WebServlet("/MasterQuickServlet")
public class MasterQuickServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MasterQuickServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		PrintWriter out = response.getWriter();
		String languages = "",mst="";
		String lang[]=request.getParameterValues("voucher2");
		String ms[]=request.getParameterValues("master");
		String updatecust="",updatemaster="";

int j=0,k=0,l=0;
		DBConnection dbc=new DBConnection();	
		 Connection con=dbc.getNewConnection();
		 
		 Statement st = null;
		 ResultSet rs = null;
		 String r1="";
		 
		 if (request.getParameter("first") != null) {
		 try 
			{
				st = con.createStatement();
				
				for(int i=0;i<lang.length;i++){
				    languages=lang[i];
				   
				    try{
				    rs=st.executeQuery("select cname from customermaster where ccode ='"+languages+"' ");
				    while(rs.next()) 
					{ 
						r1=rs.getString(1);
						}}
				    catch(Exception e1)
				    {
				    	
				    }
				    try{
					    rs=st.executeQuery("select sname from suppliermaster where scode ='"+languages+"' ");
					    while(rs.next()) 
						{ 
							r1=rs.getString(1);
							}}
					    catch(Exception e2)
					    {
					    	
					    }
				    try{
					    rs=st.executeQuery("select bname from bankmaster where micr ='"+languages+"' ");
					    while(rs.next()) 
						{ 
							r1=rs.getString(1);
							}}
					    catch(Exception e3)
					    {
					    	
					    }
				     updatecust = "insert into masterquick(mcode,master) values('"+languages+"','"+r1+"');";
				     j=st.executeUpdate(updatecust);
					
				    }
				
				
				
		
				if(j>0){
					 RequestDispatcher requestDispatcher; 
				        requestDispatcher = request.getRequestDispatcher("QuickFunction.jsp" ) ;
				        requestDispatcher.forward(request,response);
					}
					else
		               out.print("Sorry..!! Got an exception.");
				
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		 }
		 
		 else if (request.getParameter("second") != null) {
			 
			 
			 try 
				{
					st = con.createStatement();
					
					for(int i=0;i<lang.length;i++){
					    languages=lang[i];
					   
					   
					     updatecust = "delete from masterquick where mcode ='"+languages+"';";
					     j=st.executeUpdate(updatecust);
						
					    }
					
					
					
			
					if(j>0){
						 RequestDispatcher requestDispatcher; 
					        requestDispatcher = request.getRequestDispatcher("QuickFunction.jsp" ) ;
					        requestDispatcher.forward(request,response);
						}
						else
			               out.print("Sorry..!! Got an exception.");
					
				}
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
			 
		 }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
